<template>
  <div class="rollContainer">
    <div class="searchHead" ref="searchHead">
      <!-- <Form ref="formInline" :model="conditions" inline :label-width="120" :show-message="false">
        <div class="searchBox">
          <div class="conditionBox">
            <Row>
              <Col :span="6">
                <FormItem label="姓名：">
                  <Input v-model.trim="conditions.username" clearable placeholder></Input>
                </FormItem>
              </Col>
            </Row>
          </div>
          <div class="searchBtnBox">
            <Button type="primary" @click="search()">搜索</Button>
            <Button @click="handleReset()">重置</Button>
          </div>
        </div>
      </Form>-->
      <div class="tableBtnBox">
        <Button icon="md-add" size="default" @click="Publish">新增服务团队</Button>
      </div>
    </div>
    <Table :columns="columns1" :height="tableHeight" border :data="list" :loading="loading">
      <template slot-scope="{ row }" slot="avatarUrl">
        <img class="avatar" v-if="row.avatarUrl" :src="domain1+row.avatarUrl" alt />
      </template>
      <template slot-scope="{ row }" slot="action">
        <span class="tableBtn" @click="show(row)">
          <Icon size="18" type="ios-create-outline" />编辑
        </span>
        <span class="tableBtn" @click="remove(row)">
          <Icon size="18" type="ios-trash" />删除
        </span>
      </template>
    </Table>
    <div class="pageBox">
      <Page
        :total="total"
        :page-size="pageSize"
        :current="page"
        size="small"
        show-total
        show-sizer
        @on-change="changePage"
        @on-page-size-change="changeSize"
      />
    </div>
    <Modal v-model="isShow" title="服务团队新增/编辑" width="1000">
      <Form
        ref="formInline"
        :model="formInline"
        :rules="ruleInline"
        inline
        :label-width="100"
        fullscreen
      >
        <FormItem prop="name" label="昵称">
          <Input type="text" v-model.trim="formInline.name" placeholder="请输入昵称" style="width：300px"></Input>
        </FormItem>
        <FormItem prop="mobile" label="手机号">
          <Input
            type="text"
            v-model.trim="formInline.mobile"
            placeholder="请输入手机号"
            style="width：300px"
          ></Input>
        </FormItem>
        <FormItem prop="email" label="邮箱">
          <Input
            type="text"
            v-model.trim="formInline.email"
            placeholder="请输入邮箱"
            style="width：300px"
          ></Input>
        </FormItem>
        <FormItem label="服务范围：" prop="service">
          <Input
            type="text"
            v-model.trim="formInline.service"
            placeholder="请输入服务范围"
            style="width：300px"
          ></Input>
          <!-- <Select
            v-model="formInline.service"
            clearable
            @on-change="checkStatus($event, 'service')"
          >
            <Option v-for="item in typeLists" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>-->
        </FormItem>
        <FormItem prop="avatarUrl" label="头像">
          <upload
            limit="1"
            @uploadImg="uploadImg($event,'avatarUrl',multiple=false)"
            :imgs="formInline.avatarUrl"
          ></upload>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button @click="isShow=false">取消</Button>
        <Button type="primary" @click="sure('formInline')">确认</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import { teamlist, addTeam, modifyTeam } from "../../api/service";
import { _ValidatePhone } from "@/assets/js/validate.js";
import upload from "@/components/upload.vue";
export default {
  components: {
    upload
  },
  data() {
    return {
      limit: 1, //允许上传的数量
      typeLists: [
        {
          value: "1",
          label: "宣传推广"
        },
        {
          value: "2",
          label: "招展招商"
        },
        {
          value: "3",
          label: "会议论坛"
        },
        {
          value: "4",
          label: "媒体合作"
        }
      ], //服务类型
      list: [],
      page: 1,
      pageSize: 20,
      total: 0,
      loading: false,
      conditions: {
        typeId: ""
      },
      columns1: [
        {
          title: "id",
          key: "id",
          align: "center",
          minWidth: 200
        },
        {
          title: "姓名",
          key: "name",
          align: "center",
          minWidth: 200
        },
        {
          title: "头像",
          slot: "avatarUrl",
          align: "center",
          minWidth: 200
        },
        {
          title: "手机号",
          key: "mobile",
          align: "center",
          minWidth: 200
        },
        {
          title: "邮箱",
          key: "email",
          align: "center",
          minWidth: 200
        },
        {
          title: "操作",
          slot: "action",
          width: 200,
          align: "center"
        }
      ],
      tableHeight: 100,
      isShow: false,
      isFixed: false,
      username: "",
      id: "", //id
      formInline: {
        name: "",
        mobile: "",
        service: "",
        avatarUrl: "",
        email: ""
      },
      ruleInline: {
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur"
          }
        ],
        avatarUrl: [
          {
            required: true,
            message: "请上传LOGO",
            trigger: "change"
          }
        ],
        mobile: [
          {
            required: true,
            validator: _ValidatePhone,
            trigger: "blur"
          }
        ],
        service: [
          { required: true, message: "请选择服务类型", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getTeamList();
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight =
        window.innerHeight - this.$refs.searchHead.offsetHeight - 220;
    });
  },
  methods: {
    // 上传文件
    uploadImg(imgs, k, multiple) {
      if (multiple) {
        this.formInline[k] = JSON.parse(imgs);
      } else {
        this.formInline[k] = JSON.parse(imgs)[0];
      }
    },
    checkStatus(e, name) {
      this.formInline[name] = e;
      console.log(this.formInline);
    },
    changeStatus(e, name) {
      this.page = 1;
      this.conditions[name] = e;
      this.getTeamList();
    },
    getTeamList() {
      this.loading = true;
      const { page, pageSize } = this;
      teamlist(
        Object.assign(this.conditions, { page: page - 1, pageSize })
      ).then(res => {
        if (res.status) {
          this.total = res.data.count;
          this.list = res.data.list;
          this.loading = false;
        } else {
          this.$Message.error(res.msg);
          this.loading = false;
        }
      });
    },
    show(row) {
      this.id = row.id;
      this.formInline = {
        name: row.name,
        mobile: row.mobile,
        service: row.service,
        avatarUrl: row.avatarUrl,
        email: row.email
      };
      this.isShow = true;
    },
    changePage(e) {
      this.page = e;
      this.getTeamList();
    },
    changeSize(e) {
      this.page = 1;
      this.pageSize = e;
      this.getTeamList();
    },
    Publish() {
      this.id = "";
      this.formInline = {
        name: "",
        mobile: "",
        service: "",
        avatarUrl: "",
        email: ""
      };
      this.isShow = true;
    },
    sure(nam) {
      const { name, mobile, service, avatarUrl, email } = this.formInline;
      this.$refs[nam].validate(valid => {
        if (valid) {
          if (this.id == "") {
            addTeam({
              name,
              mobile,
              service,
              avatarUrl,
              email
            }).then(res => {
              console.log(res);
              if (res.status) {
                this.isShow = false;
                this.getTeamList();
                this.$Message.success(res.msg);
              } else {
                this.$Message.error(res.msg);
              }
            });
          } else {
            modifyTeam({
              id: this.id,
              name,
              mobile,
              service,
              avatarUrl,
              email
            }).then(res => {
              if (res.status) {
                this.isShow = false;
                this.getTeamList();
                this.$Message.success(res.msg);
              } else {
                this.$Message.error(res.msg);
              }
            });
          }
        } else {
        }
      });
    },
    //删除服务团队
    remove(row) {
      let json = {
        id: row.id,
        name: row.name,
        mobile: row.mobile,
        service: row.service,
        avatarUrl: row.avatarUrl,
        email: row.email,
        status: 1
      };
      this.$Modal.confirm({
        title: "警告",
        content: `删除后无法恢复，确定要删除？`,
        onOk: () => {
          modifyTeam(json).then(res => {
            if (res.status) {
              this.getTeamList();
              this.$Message.success("删除成功");
            } else {
              this.$Message.error(res.msg);
            }
          });
        },
        onCancel: () => {}
      });
    },
    cancel() {
      this.isShow = false;
    },
    editorChange(e) {},
    search() {
      this.getTeamList();
    },
    handleReset() {
      this.conditions = {
        typeId: ""
      };
      this.page = 1;
      this.getTeamList();
    }
  }
};
</script>
<style lang="less" scoped>
.page {
  display: flex;
  justify-content: flex-end;
  padding: 20px 0;
}
.avatar {
  width: 100px;
  height: 100px;
}
</style>

